set -e

##################################################################
### banner
##################################################################
echo ""
echo ""
DESCRIPTION_TEXT="This is a shell script for building dynamorio on win11"
echo "##################################################################"
echo "### ${DESCRIPTION_TEXT}"
echo "##################################################################"
echo ""


##################################################################
### functions
##################################################################

Usage() {
    echo ""
    echo ${DESCRIPTION_TEXT}
    echo "Usage:"
    echo "  sh  <shell-file-path> <Options>"
    echo ""
    echo "Options:"
    echo "  -h | --help                     = Usage."
    echo "  -c | --clean-mode               = Clean dir set"
    echo "  -t                              = test mode for clean dir set"
    echo "  -w | --workspace-dir            = workspace dirs, it also is cmakelist dir."
    echo "  -b | --build-workspace-dir      = build workspace dirs"
    echo "  -d | --deploy-workspace-dir     = deploy workspace dirs."
    echo "  -i | --install-workspace-dir    = install workspace dirs."
    echo "  -x | --is-64bit                 = is 64 bit programe."
    echo "  -z | --is-debug                 = is debug."
    echo "  -g | --generators               = generator set. Details see \`DEFAULT_GENERATORS'"
    echo "  --arch-config                   = arch config."
    echo "  --debug-config                  = debug config."
    echo ""
    exit $1
}

cd_with_print() {
    echo "-- Enter dir: $1"
    cd $1
}


##################################################################
### getopt
##################################################################
echo ""
echo ""
echo "### getop ##################################################"

CLEAN_DIRS=()
WORKSPACE_DIR=""
BUILD_WORKSPACE_DIR=""
DEPLOY_WORKSPACE_DIR=""
GENERATORS=()
IS_64BIT=0
IS_DEBUG=0
ARCH_CONFIG=""
TEST_FOR_CLEAN_MODE=0

# "$@"有引号可以解决空格问题
echo $#
echo $@
eval set -- $(getopt -o hc:tw:b:d:xzg: -l arch-config:,debug-config: -- "$@")
while [ -n "$1" ]
do
    echo "\$1: $1"
    echo "\$2: $2"

    case "$1" in
        -h | --help)
            Usage 0
            shift
            ;;
        -c | --clean-mode)
            CLEAN_DIRS[${#CLEAN_DIRS[*]}]="$2"
            shift 2
            ;;
        -t)
            TEST_FOR_CLEAN_MODE=1
            shift
            ;;
        -w)
            WORKSPACE_DIR=$2
            shift 2
            ;;
        -b)
            BUILD_WORKSPACE_DIR=$2
            shift 2
            ;;
        -d)
            DEPLOY_WORKSPACE_DIR=$2
            shift 2
            ;;
        -x)
            IS_64BIT=1
            shift
            ;;
        -Z)
            IS_DEBUG=1
            shift
            ;;
        -g)
            GENERATOR=$2
            GENERATOR=${GENERATOR//\"/}
            shift 2
            ;;
        --arch-config)
            ARCH_CONFIG=$2
            shift 2
            ;;
        --debug-config)
            DEBUG_CONFIG=$2
            shift 2
            ;;
        --)
            echo "$2"
            shift
            break
            ;;
        *)
            echo "$1 is not option"
            Usage 1
            ;;
    esac
done

echo "CLEAN_DIRS:               ${CLEAN_DIRS[*]}"
echo "CLEAN_DIR_COUNT:          ${#CLEAN_DIRS[*]}"
echo "TEST_FOR_CLEAN_MODE:      ${TEST_FOR_CLEAN_MODE}"
echo "WORKSPACE_DIR:            $WORKSPACE_DIR"
echo "BUILD_WORKSPACE_DIR:      $BUILD_WORKSPACE_DIR"
echo "DEPLOY_WORKSPACE_DIR:     $DEPLOY_WORKSPACE_DIR"
echo "GENERATOR:                ${GENERATOR}"
echo "IS_64BIT:                 $IS_64BIT"
echo "IS_DEBUG:                 $IS_DEBUG"
echo "ARCH_CONFIG:              $ARCH_CONFIG"
echo "DEBUG_CONFIG:             $DEBUG_CONFIG"


##################################################################
### Clean
##################################################################

echo ""
echo ""
echo "### Clean ##################################################"

RESULT=""
for val in ${CLEAN_DIRS[*]}; do
    rm_dir="${val}"
    if [ -d "$rm_dir" ]; then
        if [ $TEST_FOR_CLEAN_MODE == 0 ]; then
            rm -rf "$rm_dir" && RESULT="Pass" || RESULT="Fail"
        else
            RESULT="Pass"
        fi
    else
        RESULT="Skip"
    fi
    echo "${RESULT}:  rm -rf $rm_dir"
done

if [ $TEST_FOR_CLEAN_MODE == 1 ]; then
    echo "Exit for test!"
    exit 0
fi


##################################################################
### generate makefile
##################################################################
echo ""
echo ""
echo "### generate makefile ######################################"

export PROJECT_NAME=jc_first_fuzz_sample

mkdir -p "${BUILD_WORKSPACE_DIR}"
cd_with_print "${BUILD_WORKSPACE_DIR}"

date

echo "Use ${GENERATOR}"

# cmake \
# -G "${GENERATOR}" \
# -A ${ARCH_CONFIG} \
# -DCMAKE_BUILD_TYPE=${DEBUG_CONFIG} \
# -DCMAKE_INSTALL_PREFIX="${DEPLOY_WORKSPACE_DIR}" \
# -DBUILD_DOCS=OFF  \
# "${WORKSPACE_DIR}" \
# || (echo "Error at generate makefile"; exit 1;)


CMDs=(
    cmake
    -G "\"${GENERATOR}\""
    -A ${ARCH_CONFIG}
    -DCMAKE_BUILD_TYPE=${DEBUG_CONFIG}
    -DCMAKE_INSTALL_PREFIX="${DEPLOY_WORKSPACE_DIR}"
    -DBUILD_DOCS=OFF
    "${WORKSPACE_DIR}"
)

# echo cmd
echo -e "\n=== CMDs ========================================="
CMDs_LEN=${#CMDs[*]}
for (( i=0; i<${CMDs_LEN}; i++ )); do
    if  (( ${i}+1 == ${CMDs_LEN} )); then
        echo "${CMDs[$i]}"
    else
        echo "${CMDs[$i]} \\"
    fi
    CMD_EXEC+="${CMDs[$i]} "
done
echo ""

echo -e "=== CMD_EXEC ========================================="
echo -e "${CMD_EXEC}\n"

### run cmd
sh -c "${CMD_EXEC}"

date


##################################################################
### building
##################################################################
echo ""
echo ""
echo "### build ##################################################"
date
cmake --build ./ --config "${DEBUG_CONFIG}" -- -m || exit 1
date


##################################################################
### installing
##################################################################
echo ""
echo ""
echo "### install ################################################"
mkdir -p "${DEPLOY_WORKSPACE_DIR}"
date
cmake --install ./ --config "${DEBUG_CONFIG}"  || exit 1
date


##################################################################
### The end
##################################################################
echo ""
echo ""
echo "### The end ################################################"
echo ""
echo "Here is end, then you can delete build directory manually."
echo ""
ls "${DEPLOY_WORKSPACE_DIR}"
echo ""

echo "-- ls ${DEPLOY_WORKSPACE_DIR}/bin"
ls "${DEPLOY_WORKSPACE_DIR}/bin"
